var ajaxMore;
/**
 * 把课程包名称用于构造html内容
 */

function makeHtmlContent(className) {
    var htmlContent='<div style="width:100%;margin-top: 20px;">'+
        '<p style="font-size: 24px;">尊敬的用户，您好：</p>'+
        '<p style="font-size: 24px;">'+
        '你所购买的《'+className+'》已超过课程服务期，请扫下方二维码下载红蚂蚁机构APP，点击首页右上角的找老师进行相关问题咨询。'+
        '</p>'+
        '<div style="text-align: center;margin-top: 60px;">'+
        '<img style="margin-right: 80px;margin-top: 140px;width: 210px;" src="images/app_qr_tips.png?v=20170821">'+
        '<img style="margin-left: 80px;width: 252px;" src="images/app_homepage_screenshot.jpeg">'+
        '</div>'+
        '</div>';
    return htmlContent;
}
/**
 * 我的课程
 */
function queryPackageList(userId){
	$.ajax({
		url: "new_pt_uc/my_lesson/packageList.action",
		type: "post",
		data:{
			"userId":userId
		},
		cache: false,
		beforeSend:function(XMLHttpRequest){
			$("#loading").show(); 
		},
		complete:function(XMLHttpRequest){ 
           $("#loading").hide(); 
         },
		success: function(data){
			$('#packageDiv').html(data);
			ajaxMore = $("#ajaxMoreClass").html();
		}
	});
}

/**
 * 查询直播课表
 * packageId:其实是ordDetailId
 */
function queryLiveLesson(packageId,ordDetailId,isOld,userId,dateFlag){
	$('#ajaxMoreClass').css('display','none');
	hidMyLessonDiv('currentWeekLiveDiv');
	$("#currentWeekLiveDiv").html("");
	$('#ajaxMoreClass').html(ajaxMore);
	$.ajax({
		url: "new_pt_uc/my_lesson/queryLiveLesson.action",
		type: "post",
		data:{
			"packageId":packageId,
			"ordDetailId":ordDetailId,
			"isOld":isOld,
			"userId":userId,
			"dateFlag":dateFlag
			},
		cache: false,
		beforeSend:function(XMLHttpRequest){
			$("#loading").show(); 
		},
		success: function(data){
			$("#currentWeekLiveDiv").html(data);
		},
		complete:function(XMLHttpRequest){ 
           $("#loading").hide(); 
         }
	});
	
}

/**
 * 点击课程包事件
 */
function queryPackageId(packageId,ordDetailId,isOld,hasReserve,userId,expired,className){
	$('#ajaxMoreClass').css('display','none');
	$("#funDiv").show();
	$.ajax({
		url: "new_pt_uc/my_lesson/packageId.action",
		type: "post",
		data:{
			"packageId":packageId,
			"ordDetailId":ordDetailId,
			"isOld":isOld,
			"hasReserve":hasReserve,
			"userId":userId,
			"expired":expired,
			"className":className
			},
		cache: false,
		success: function(data){
			$('#package'+((packageId-0)+(ordDetailId-0))).addClass('mc-cur').siblings('li').removeClass('mc-cur');
			$("#funDiv").html(data);
			$('.mc-cur').prependTo('.mc-header-tab-scroll2 .mc-content .mc-header-tab2').show();
			$('.mc-header-tab-scroll2 .mc-content li:gt(2)').hide();
			var funName="";
			if (getCookie("fun"+userId)!=null) {
				var fun = getCookie("fun" + userId).split(";");
				if(fun[1]==ordDetailId){
					funName=fun[0];
				}
			}

			saveFun(packageId,ordDetailId,isOld,hasReserve,userId,funName,expired,className);
		}
	});
}

/**
 * 查询重播课表模块
 */
function queryReviewModel(packageId,ordDetailId,isOld,userId){
	hidMyLessonDiv('reviewDiv');
	$("#reviewDiv").html("");
	$.ajax({
		url: "new_pt_uc/lesson_details/reviewModel.action",
		type: "post",
		data:{
			"packageId":packageId,
			"ordDetailId":ordDetailId,
			"isOld":isOld,
			"userId":userId
			},
		cache: false,
		beforeSend:function(XMLHttpRequest){
			$("#loading").show(); 
		},
		success: function(data){
			$("#reviewDiv").html(data);
		},
		complete:function(XMLHttpRequest){ 
           $("#loading").hide(); 
         }
	});
	
}
/**
 * 查询重播课表
 */
function queryReviewLesson(mouduleId,typeCode,liveRecordId,ordDetailId,userId){
	if ($("#reviewLesson" + mouduleId).html().length==0) {
		$.ajax({
			url : "new_pt_uc/lesson_details/reviewLesson.action",
			type : "post",
			data : {
				"mouduleId" : mouduleId,
				"typeCode" : typeCode,
				"liveRecordId":liveRecordId,
				"ordDetailId":ordDetailId,
				"userId":userId
			},
			cache : false,
			beforeSend:function(XMLHttpRequest){
				$("#loading").show(); 
			},
			success : function(data) {
				$("#reviewLesson" + mouduleId).html(data);
			},
			complete:function(XMLHttpRequest){ 
	           $("#loading").hide(); 
	         }
		});
	}
	
}
/**
 * 查询旧版个人中心重播课表
 */
function queryReviewOldLesson(mouduleId,typeCode,classIds,liveRecordId){
	if ($("#reviewLesson" + mouduleId).html().length==0) {
		$.ajax({
			url : "new_pt_uc/lesson_details/reviewOldLesson.action",
			type : "post",
			data : {
				"typeCode" : typeCode,
				"classIds" : classIds,
				"liveRecordId":liveRecordId
			},
			cache : false,
			beforeSend:function(XMLHttpRequest){
				$("#loading").show(); 
			},
			success : function(data) {
				$("#reviewLesson" + mouduleId).html(data);
			},
			complete:function(XMLHttpRequest){ 
	           $("#loading").hide(); 
	         }
		});
	}
	
}

function hidMyLessonDiv(divType){
	if(divType == 'currentWeekLiveDiv'){
		$("#reviewDiv").hide();
		$("#tikuDiv").hide();
		$("#datumBundleDiv").hide();
		$("#freeRecordDiv").hide();
		$("#keyComplainDiv").hide();
		$("#reserveDiv").hide();
	}else if(divType == 'reviewDiv'){
		$("#currentWeekLiveDiv").hide();
		$("#tikuDiv").hide();
		$("#datumBundleDiv").hide();
		$("#freeRecordDiv").hide();
		$("#ajaxMoreClass").hide();
		$("#noMoreCourse").hide();
		$("#keyComplainDiv").hide();
		$("#reserveDiv").hide();
	}else if(divType == 'tikuDiv'){
		$("#currentWeekLiveDiv").hide();
		$("#reviewDiv").hide();
		$("#datumBundleDiv").hide();
		$("#freeRecordDiv").hide();
		$("#ajaxMoreClass").hide();
		$("#noMoreCourse").hide();
		$("#keyComplainDiv").hide();
		$("#reserveDiv").hide();
	}else if(divType == 'datumBundleDiv'){
		$("#currentWeekLiveDiv").hide();
		$("#reviewDiv").hide();
		$("#tikuDiv").hide();
		$("#freeRecordDiv").hide();
		$("#ajaxMoreClass").hide();
		$("#noMoreCourse").hide();
		$("#keyComplainDiv").hide();
		$("#reserveDiv").hide();
	}else if(divType == 'freeRecordDiv'){
		$("#currentWeekLiveDiv").hide();
		$("#reviewDiv").hide();
		$("#tikuDiv").hide();
		$("#datumBundleDiv").hide();
		$("#ajaxMoreClass").hide();
		$("#noMoreCourse").hide();
		$("#keyComplainDiv").hide();
		$("#reserveDiv").hide();
	}else if(divType == 'keyComplainDiv'){
		$("#currentWeekLiveDiv").hide();
		$("#reviewDiv").hide();
		$("#tikuDiv").hide();
		$("#datumBundleDiv").hide();
		$("#ajaxMoreClass").hide();
		$("#noMoreCourse").hide();
		$("#freeRecordDiv").hide();
		$("#reserveDiv").hide();
	}else if(divType == 'reserveDiv'){
		$("#loading").hide(); 
		$("#currentWeekLiveDiv").hide();
		$("#reviewDiv").hide();
		$("#tikuDiv").hide();
		$("#datumBundleDiv").hide();
		$("#ajaxMoreClass").hide();
		$("#noMoreCourse").hide();
		$("#freeRecordDiv").hide();
		$("#keyComplainDiv").hide();
	}
	
	$("#"+divType).show();
	$('.mc-sub-tab a').removeClass('mc-sub-tab-cur');
	$("#"+divType+"Sub").addClass('mc-sub-tab-cur');
}

/**
 * 下载录音
 */


function getAudioDownloadUrl(playWebcastid){
	$.ajax({
		url: "pt_uc/live/ajaxGetAudioDownloadUrl.action",
		type:"post",
		data:{"playWebcastid":playWebcastid},
		cache: false,
		success:function(data){
			if(data.code == 0){
				window.location.href = data.url;
			}else{
				alert('音频正在制作中，请稍候……');
				return false;
			}
		}
	});	
	return false;
}

function downloadFile(fileUrl,attId){
	var filePath = 'http://static.sunlands.com'+fileUrl;
	 $.ajax({
		url: "new_pt_uc/lesson_details/ajaxCheckUrl.action",
		type:"post",
		data:{"url":filePath,"attId":attId},
		cache: false,
		success:function(data){
			var validResult = data.validResult;
			if(validResult){
				window.location.href = filePath;
				$("#hasDownLoad" + attId).attr("class","cr-attendance");
				$("#hasDownLoad" + attId).text("已下载");
			}else{
				window.location.href = "new_pt_uc/lesson_details/downLoadAttachmentById.action?attrId="+attId;
			}
		}
	});	
	return false;
}

/**
 * 查询我的课程包题库
 */
function queryTiku(packageId,ordDetailId,isOld,userId){
	hidMyLessonDiv('tikuDiv');
	$("#tikuDiv").html("");
	$.ajax({
		url: "new_pt_uc/my_tiku/tikuList.action",
		type: "post",
		data:{
			"packageId":packageId,
			"ordDetailId":ordDetailId,
			"isOld":isOld,
			"userId":userId
			},
		cache: false,
		beforeSend:function(XMLHttpRequest){
			$("#loading").show(); 
		},
		success: function(data){
			$("#tikuDiv").html(data);
		},
		complete:function(XMLHttpRequest){ 
           $("#loading").hide(); 
         }
	});
	
}

/**
 * 查询我的资料库
 */
function queryDatumBundle(packageId,ordDetailId,isOld){
	hidMyLessonDiv('datumBundleDiv');
	$.ajax({
		url: "new_pt_uc/soft_download/showSoftDownload.action",
		type: "post",
		data:{
			"packageId":packageId,
			"ordDetailId":ordDetailId,
			"isOld":isOld
			},
		cache: false,
		success: function(data){
			$("#datumBundleDiv").html(data);
		}
	});
	
}

/**
 * 查询我的资料
 */
function queryDatum(packageId,bundleId,isOld){
	if ($("#datum"+packageId+bundleId).html().length==0) {
		$.ajax({
			url: "new_pt_uc/soft_download/showSoftDownloadByDatumBundleId.action",
			type: "post",
			data:{
				"bundleId":bundleId,
				"isOld":isOld
				},
			cache: false,
			success: function(data){
				$("#datum"+packageId+bundleId).html(data);
			}
		});
	}
	
}

/**
 * 查询赠送录播课模块
 */
function queryFreeRecord(packageId,ordDetailId,isOld,userId){
	hidMyLessonDiv('freeRecordDiv');
	$.ajax({
		url: "new_pt_uc/lesson_details/freeRecordInfo.action",
		type: "post",
		data:{
			"packageId":packageId,
			"ordDetailId":ordDetailId,
			"isOld":isOld,
			"userId":userId
			},
		cache: false,
		success: function(data){
			$("#reviewDiv").html('');
			$("#freeRecordDiv").html(data);
		}
	});
}

/**
 * 查询赠送录播课表
 */
function queryFreeRecordLesson(mouduleId){
	if (!$("#freeRecordModule"+mouduleId).hasClass("cr-open")&&$("#freeRecordLesson"+mouduleId).html()=="") {
		$.ajax({
			url: "new_pt_uc/lesson_details/freeRecordLesson.action",
			type: "post",
			data:{"mouduleId":mouduleId},
			cache: false,
			success: function(data){
				$("#freeRecordLesson"+mouduleId).html(data);
			}
		});
	}
	
}

function ajaxSelectWorkCount(){
	var modelWorkInfos = "";
	for (var i = 0; i < $("input[name='modelWorkInfo']").length; i++) {
		modelWorkInfos+=$("input[name='modelWorkInfo']")[i].value+";";
	}
	$.ajax({
		url: "new_pt_uc/lesson_details/ajaxSelectWorkCount.action",
		type: "post",
		data:{"modelWorkInfos":modelWorkInfos},
		dataType: "json",
		success: function(data){
			if(data.success){
				if(null != data.moduleDTOs){
					for(var i = 0 ;i<data.moduleDTOs.length;i++){
						$("#undoneWork"+data.moduleDTOs[i].id).html(data.moduleDTOs[i].undoneWorkcount);
					}
				}
			}
		}
	});
	
}

/**
 * 一键投诉
 */
function queryComplaint(packageId, ordDetailId,userId){
	hidMyLessonDiv('keyComplainDiv');
	$.ajax({
		url: "new_pt_uc/complain/complainQuestions.action",
		data:{
			"packageId":packageId,
			"ordDetailId":ordDetailId,
			"userId":userId
			},
		type: "post",
		cache: false,
		success: function(data){
			$('#keyComplainDiv').html(data);
		}
	});
//	$.ajax({
//		url: "new_pt_uc/complain/complains.action",
//		data:{
//			"packageId":packageId,
//			"ordDetailId":ordDetailId,
//			"userId":userId
//		},
//		type: "post",
//		cache: false,
//		success: function(data){
//			$('#keyComplainDiv').html(data);
//		}
//	});
	
}

/**
 * 约课
 */
function queryReserve(ordDetailId){
	hidMyLessonDiv('reserveDiv');
	$.ajax({
		url: "new_pt_uc/reserve/reserveTab.action",
		type: "post",
		data:{
			"ordDetailId":ordDetailId
			},
		cache: false,
		success: function(data){
			$("#reserveDiv").html(data);
			getReserveCourseList(0,ordDetailId);
		}
	});
}
/**
 * 约课信息
 */
function getReserveCourseList(queryType,ordDetailId){
	$("#reserve").html("");
	var conditionType=null,queryContent=null,startTime=null,endTime=null;
	if(queryType==0){
		conditionType=$("#conditionType").val();
		queryContent=$("#queryContent").val();
		startTime=$("#startTime").val();
		endTime=$("#endTime").val();
		$.ajax({
			url: "new_pt_uc/reserve/getStuReserveCourseNum.action",
			type: "post",
			data:{
				"ordDetailId":ordDetailId
			},
			cache: false,
			success: function(data){
				$("#reserveNum").html(data.count);
			}
		});
	}
	$.ajax({
		url: "new_pt_uc/reserve/getReserveCourseList.action",
		type: "post",
		async: false,
		data:{
			"ordDetailId":ordDetailId,
			"queryType":queryType,
			"conditionType":conditionType,
			"queryContent":queryContent,
			"startTime":startTime,
			"endTime":endTime
			},
		cache: false,
		success: function(data){
			$("#reserve").html(data);
		}
	});
}
/**
 * 预约
 */
function stuReserveCourse(reserveCourseId,ordDetailId,courseName,deadline,teachers){
	$.ajax({
		url: "new_pt_uc/reserve/validateReserveCourse.action",
		async: false,
		type: "post",
		data:{
			"ordDetailId":ordDetailId
		},
		cache: false,
		success: function(data){
			if(data.flag==1){
				if(data.usableCount>0){
					$.ajax({
						url: "new_pt_uc/reserve/stuReserveCourse.action",
						type: "post",
						async: false,
						data:{
							"reserveCourseId":reserveCourseId,
							"ordDetailId":ordDetailId
						},
						cache: false,
						success: function(data){
							if(data.flag==1){
								$("#reserveMsg").html("您已预约"+teachers+"老师的"+courseName+"，预约结果请在"+deadline+"关注短信通知，祝您学习愉快！");
								$('#ordered').show().fadeOut(5000);
								setTimeout(function() {
									getReserveCourseList(0,ordDetailId);
								}, 3000);
							}else{
								$("#reserveMsg").html(data.message);
							}
							$('#ordered').show().fadeOut(5000);
						}
					});
				}else{
					if(data.limitType=='CONCURRENT_REQ_LIMIT'){
						$("#reserveMsg").html("您的预约可以满足院校提面的需求。请您合理安排预约次数，为其他目标院校留出资源。");
						$('#ordered').show().fadeOut(5000);
					}else if(data.limitType=='MAX_COUNT'){
						$("#reserveMsg").html("您的预约次数已用完！");
						$('#ordered').show().fadeOut(5000);
					}else{
						$("#reserveMsg").html("预约失败！");
						$('#ordered').show().fadeOut(5000);
					}
				}
			}else{
				$("#reserveMsg").html("预约失败！");
				$('#ordered').show().fadeOut(5000);
			}
		}
	});
}
/**
 * 取消预约
 */
function stuCancelReserveCourse(stuReserveCourseId,ordDetailId,courseName,attendClassDate,teachers){
	if (confirm("您确认要取消"+attendClassDate+teachers+"老师的"+courseName+"吗？")) {
		var reserveNum = $("#reserveNum").html();
		reserveNum++;
		$.ajax({
			url : "new_pt_uc/reserve/stuCancelReserveCourse.action",
			type : "post",
			data : {
				"stuReserveCourseId" : stuReserveCourseId
			},
			cache : false,
			success : function(data) {
				if (data.flag == 1) {
					$("#cancelReserveMsg").html(
							"您已取消" + teachers + "老师的" + courseName
									+ "预约，您还可以再预约" + reserveNum + "次！");
					$('#cancelOrder').show().fadeOut(5000);
					setTimeout(function() {
						getReserveCourseList(1, ordDetailId);
					}, 3000);
				} else {
					$("#cancelReserveMsg").html("取消预约失败！");
				}
				$('#cancelOrder').show().fadeOut(5000);
			}
		});
	}
}
function saveFun(packageId,ordDetailId,isOld,hasReserve,userId,funName,expired,className){
	if(funName=="currentWeekLiveDivSub"){
		if(expired=='EXPIRED'){
			var htmlContent=makeHtmlContent(className);
            hidMyLessonDiv('currentWeekLiveDiv');
            $("#currentWeekLiveDiv").html(htmlContent);
		}else{
            $("#reviewDiv").html("");
            queryLiveLesson(packageId,ordDetailId,isOld,userId,0);
		}
	}else if(funName=="reviewDivSub"){
		$("#currentWeekLiveDiv").html("");
		queryReviewModel(packageId,ordDetailId,isOld,userId);
	}else if(funName=="tikuDivSub"){
		queryTiku(packageId,ordDetailId,isOld,userId);
	}else if(funName=="datumBundleDivSub"){
		queryDatumBundle(packageId,ordDetailId,isOld);
	}else if(funName=="freeRecordDivSub"){
		queryFreeRecord(packageId,ordDetailId,isOld,userId);
	}else if(isOld==0&&funName=="keyComplainDivSub"){
		queryComplaint(packageId,ordDetailId,userId);
	}else if(isOld==0&&funName=="reserveDivSub"&&hasReserve==1){
		queryReserve(ordDetailId);
	}else{
        if(expired=='EXPIRED'){
            var htmlContent=makeHtmlContent(className);
            hidMyLessonDiv('currentWeekLiveDiv');
            $("#currentWeekLiveDiv").html(htmlContent);
        }else{
            queryLiveLesson(packageId,ordDetailId,isOld,userId,0);
        }
	}
	setCookie("fun"+userId,funName+";"+ordDetailId,365);
}

function query_by_lesson_name(){
	var lessonName=$('#search_content').val();
	var packageId=$('#packageId').val();
	var ordDetailId=$('#ordDetailId').val();
	var userId=$('#userId').val();
	var sortType=$('#sortType').val();
	
	$('#module_list').html("");
	$.ajax({
		url: "new_pt_uc/lesson_details/queryByLessonName.action",
		type: "post",
		data:{
			"packageId":packageId,
			"ordDetailId":ordDetailId,
			"lessonName":lessonName,
			"userId":userId,
			"sortType":sortType
			},
		cache: false,
		beforeSend:function(XMLHttpRequest){
			$("#loading").show(); 
		},
		success: function(data){
			$("#module_list").html(data);
		},
		complete:function(XMLHttpRequest){ 
           $("#loading").hide(); 
         }
	});
	
}
function sort_by_date_asc(){
	$('#sortType').val(true);
	document.cookie="sortType="+true;
	query_by_lesson_name();
}
function sort_by_date_desc(){
	$('#sortType').val(false);
	document.cookie="sortType="+false;
	query_by_lesson_name();
}
function getCookie(keyName){//获取指定名称的cookie的值 
	var arrStr = document.cookie.split("; "); 
	for(var i = 0;i < arrStr.length;i ++){ 
	var temp = arrStr[i].split("="); 
	if(temp[0] == keyName) return unescape(temp[1]); 
	} 
}
function setSortSortTypeByCookie(){
	if(getCookie("sortType")==undefined){
		$('#sortType').val(true);
		$('#radioAsc').prop("checked","checked");
	}
	if(getCookie("sortType")=="true"){
		$('#sortType').val(true);
		$('#radioAsc').prop("checked","checked");
	}
	if(getCookie("sortType")=="false"){
		$('#sortType').val(false);
		$('#radioDesc').prop("checked","checked");
		query_by_lesson_name();
	}
}
//nps、考试计划弹窗控制  (已废弃)
function ajaxSelectItem(){
	$.ajax({
		url: "new_pt_uc/my_lesson/ajaxSelectItem.action",
		type: "post",
		cache: false,
		success: function(data){
			if(data.itemCode=='EXAMPLAN'){
				//暂时加入老学员考试计划
				var osep = new OldStudentExamPlan();
				osep.showDialog();
			}else if(data.itemCode=='NPS'){
				//弹出满意度
				openSatisfactionDialog();
				
			}
		}
	});
}


